<?php

namespace App\Http\Controllers;

use App\Models\DepartmentCategory;
use App\Models\Project;
use Illuminate\Http\Request;

class DepartmentCategoryController extends Controller
{

    public function getActiveDepartment(Request $request): \Illuminate\Http\JsonResponse
    {
        $data = DepartmentCategory::query()
            ->where('depth', '<=', 2)
            ->where('is_show', 1)
            ->get();

        return response()
            ->json([
                'code' => HTTP_OK,
                'msg' => 'OK',
                'data' => $data,
            ]);
    }

    public function getProjectOfDepartment(Request $request, $id): \Illuminate\Http\JsonResponse
    {
        if (!$id)
            return response()
                ->json([
                    'code' => HTTP_BAD_REQUEST,
                    'msg' => '未知ID'
                ]);

        $data = DepartmentCategory::query()
            ->with([
                'projects' => function ($query) {
                    $query
                        ->where('depth', '<=', 2)
                        ->where('is_show', 1);
                }
            ])
            ->where('id', $id)
            ->first();

        return response()
            ->json([
                'code' => HTTP_OK,
                'msg' => 'OK',
                'data' => $data,
            ]);
    }


}
